import time

from selenium import webdriver
from selenium.webdriver.common.by import By
import  xlrd


class Login:

#########################################################################################
###################################      获取数据       ###################################
    def get_data(self,file,index):
        global book
        book = xlrd.open_workbook(filename=file)
        st = book.sheet_by_index(index)
        lengh1 = len(st.col_values(0)[:])
        data = []
        for i in range(1, lengh1):
            data.append(st.row_values(i)[:])
        return data

#########################################################################################
###################################        登录        ###################################

    def Login(self,username,password,expected,case):
    #登录方法
        global result
        option = webdriver.ChromeOptions()
        option.add_argument('--headless')
        driver = webdriver.Chrome(options=option)
        #窗口最大化
        driver.maximize_window()
        #跳转网址
        driver.get("http://localhost:90")

        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        # 获取页面标题
        title = driver.title
        #判断是否登陆成功
        if title == "度小满理财-登录[内测版]":
            result = "F"
        else:
            # 获取实际结果
            result = "T"

        if result == expected:
            print("通过")
        else:
            print("不通过！")
            driver.save_screenshot(f"{case}.png")

        # 退出浏览器
        driver.quit()
        return result

#########################################################################################
###################################        注册        ###################################

    def Register(self,username,password1,password2,expected,case):
    #注册方法
        global result
        option = webdriver.ChromeOptions()
        option.add_argument('--headless')
        driver = webdriver.Chrome(options=option)
        #窗口最大化
        driver.maximize_window()
        #跳转网址
        driver.get("http://localhost:90")
        # 进入注册页面
        driver.find_element(By.XPATH, "//div[@style='margin-left: 100px']/a").click()
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password1)
        # 输入重复密码
        driver.find_element(By.XPATH, "//input[@id='repassword']").send_keys(password2)
        #点击注册
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        # 获取页面标题
        title = driver.title
        # 判断是否注册成功
        if title == "个人理财系统注册界面":
            result = "F"
        else:
            result = "T"

        if result == expected:
            print("通过")
        else:
            print("不通过！")
            driver.save_screenshot(f"{case}.png")

        # 退出浏览器
        driver.quit()
        return result

#########################################################################################
###################################       银行卡       ###################################

    def Bank_card(self,username, password, cardname, cardtype,cardnum,category,new_cardname,new_cardtype,new_cardnum,expected,case):
        global result, flag
        flag = "0"
        driver = webdriver.Chrome()
        # 窗口最大化
        driver.maximize_window()
        # 跳转网址
        driver.get("http://localhost:90")
        time.sleep(2)
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        time.sleep(1)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        time.sleep(1)
        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
        time.sleep(1)
        # 获取当前页面银行卡数量
        tbody = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div")
        time.sleep(1)
        divs = tbody.find_elements(By.XPATH, "div")
        nums = len(divs)
        num1 = nums -3
        if category == "新增银行卡":
            # 点击新增
            driver.find_element(By.XPATH, "//*[@id='bankCard_add_modal_btn']").click()
            time.sleep(1)
            # 输入银行卡所属银行
            driver.find_element(By.XPATH, "//input[@id='cardBank_add_input']").send_keys(cardname)
            time.sleep(1)
            # 选择银行卡类别
            if cardtype == "借记卡":
                # 点击借记卡
                driver.find_element(By.XPATH, "//*[@id='bankCardAddModal']/div/div/div[2]/form/div[2]/div[1]/div/label[1]").click()
            else:
                # 点击信用卡
                driver.find_element(By.XPATH, "//*[@id='bankCardAddModal']/div/div/div[2]/form/div[2]/div[1]/div/label[2]").click()
            time.sleep(1)
            # 输入银行卡号
            cardnum = f"{int(cardnum)}"
            driver.find_element(By.XPATH, "//input[@id='cardNum_add_input']").send_keys(cardnum)
            time.sleep(1)
            # 保存
            driver.find_element(By.XPATH,"//*[@id='bankCard_save_btn']").click()
            time.sleep(1)

        elif category == "删除卡测试":
            # 最后一张银行卡点击删除
            driver.find_element(By.XPATH, f"/html/body/div/div/main/div/div/div[{num1}]/div/div/div[1]/ul/li/button[2]").click()
            time.sleep(1)
            if expected == "T":
                # 点击确定
                driver.find_element(By.XPATH, f"/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").click()
            else:
                # 点击取消
                driver.find_element(By.XPATH, f"/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[2]").click()
            time.sleep(1)
        else:
            # 最后一张银行卡点击编辑
            driver.find_element(By.XPATH, f"/html/body/div/div/main/div/div/div[{num1}]/div/div/div[1]/ul/li/button[1]").click()
            time.sleep(1)
            # 编辑银行卡名称
            if new_cardname != "":
                #编辑银行卡名称
                driver.find_element(By.XPATH, f"/html/body/div[1]/div/main/div/div/div[{nums}]/div/div/div[2]/form/div[1]/div[1]/input").clear()
                time.sleep(1)
                driver.find_element(By.XPATH, f"/html/body/div[1]/div/main/div/div/div[{nums}]/div/div/div[2]/form/div[1]/div[1]/input").send_keys(new_cardname)
                flag = "1"
            #编辑银行卡类别
            elif new_cardtype == "借记卡":
                # 编辑银行卡类别
                driver.find_element(By.XPATH, f"/html/body/div[1]/div/main/div/div/div[{nums}]/div/div/div[2]/form/div[2]/div[1]/div/label[1]/span").click()
                flag = "2"
                time.sleep(1)
            elif new_cardtype == "信用卡":
                driver.find_element(By.XPATH, f"/html/body/div[1]/div/main/div/div/div[{nums}]/div/div/div[2]/form/div[2]/div[1]/div/label[2]/span").click()
                flag = "2"
            # 编辑银行卡卡号
            elif new_cardnum != "":
                driver.find_element(By.XPATH, f"/html/body/div[1]/div/main/div/div/div[{nums}]/div/div/div[2]/form/div[3]/div[1]/input").clear()
                time.sleep(1)
                driver.find_element(By.XPATH, f"/html/body/div[1]/div/main/div/div/div[{nums}]/div/div/div[2]/form/div[3]/div[1]/input").send_keys(new_cardnum)
                flag = "3"
            else:
                print("没有获取到修改信息")
                #点击关闭
                driver.find_element(By.XPATH, f"/html/body/div[1]/div/main/div/div/div[{nums}]/div/div/div[3]/button[2]").click()
            time.sleep(1)
            try:
                #点击更新
                driver.find_element(By.XPATH, f"/html/body/div[1]/div/main/div/div/div[{nums}]/div/div/div[3]/button[2]").click()
            except:
                num1 = "0"
        time.sleep(2)
        # 获取当前页面银行卡数量
        tbody = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div")
        time.sleep(1)
        divs = tbody.find_elements(By.XPATH, "div")
        num2 = len(divs) - 3
        time.sleep(1)
        if flag != "0":
            if flag == "1":
                num1 = new_cardname
                num2 = driver.find_element(By.XPATH, f"/html/body/div/div/main/div/div/div[{num2}]/div/div/div[1]/div/h4").text[:-6]
            if flag == "2":
                num1 = new_cardtype
                num2 = driver.find_element(By.XPATH, f"/html/body/div/div/main/div/div/div[{num2}]/div/div/div[1]/div/h4").text[-4:-1]
            if flag == "3":
                num1 = new_cardnum
                num2 = driver.find_element(By.XPATH,f"/html/body/div/div/main/div/div/div[{num2}]/div/div/div[2]/h4[1]").text[3:]
            else:
                num2 = num1
        if num1 == num2:
            result = "F"
        else:
            result = "T"
        if result == expected:
            print("通过")
        else:
            print("不通过")
            driver.save_screenshot(f"{case}.png")

        # 退出浏览器
        driver.quit()
        return result

#########################################################################################
###################################        理财        ###################################

    def Management(self,username, password, payment_password,category,expected,case):
        global money,flag
        flag = 0
        # 打开谷歌浏览器
        driver = webdriver.Chrome()
        #窗口最大化
        driver.maximize_window()
        #跳转网址
        driver.get("http://localhost:90")
        time.sleep(2)
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        time.sleep(1)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        time.sleep(1)
        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id = 'leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, "//*[ @ id = 'leftbaraside']/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
        time.sleep(2)
        try:
            # 获取第一张银行卡的金额
            before = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[2]/div/div/div[2]/h4[2]").text
            time.sleep(1)
            if case == "多个银行卡-默认支付测试-第二张卡":
                try:
                    # 点击设置第一张卡位默认
                    driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[3]").click()

                except:
                    # 点击设置第二张卡位默认
                    driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[3]/div/div/div[1]/ul/li/button[3]").click()
                    flag = 1
                    # 获取第二张银行卡的金额
                    before = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[3]/div/div/div[2]/h4[2]").text
                time.sleep(1)
        except:
            before = None
        # 点击个人理财
        driver.find_element(By.XPATH, "//*[@id = 'leftbaraside']/div[2]/nav/ul/li[2]/a").click()
        time.sleep(1)
        if category == "零钱理财":
            # 点击零钱理财
            driver.find_element(By.XPATH, "//*[@id = 'leftbaraside']/div[2]/nav/ul/li[2]/ul/li[1]/a").click()
            time.sleep(1)
            # 获取支付宝零钱理财金额
            money = driver.find_element(By.XPATH,"/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[5]").text  # .text取出文本数据
            time.sleep(1)
            # 点击投资支付宝零钱理财
            driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button").click()

        elif category == "期限理财":
            # 点击期限理财
            driver.find_element(By.XPATH, "//*[@id = 'leftbaraside']/div[2]/nav/ul/li[2]/ul/li[2]/a").click()
            time.sleep(1)
            # 获取支付宝期限理财金额
            money = driver.find_element(By.XPATH,"/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[5]").text # .text取出文本数据
            time.sleep(1)
            # 点击投资支付宝期限理财
            driver.find_element(By.XPATH,"//html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[8]/button").click()

        elif category == "基金理财":
            # 点击基金理财
            driver.find_element(By.XPATH, "//*[@id = 'leftbaraside']/div[2]/nav/ul/li[2]/ul/li[3]/a").click()
            time.sleep(1)
            # 获取广发多元新兴股票金额
            money = driver.find_element(By.XPATH,"/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]").text  # .text取出文本数据
            time.sleep(1)
            # 点击投资广发多元新兴股票
            driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button").click()
        time.sleep(1)
        # 输入支付密码
        driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/input").send_keys(payment_password)
        time.sleep(1)
        # 点击确定
        driver.find_element(By.XPATH, "/html/body/div[3]/div[3]/a[1]").click()
        time.sleep(1)
        # 点击确定
        driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[4]/button").click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "//*[@id = 'leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, "//*[ @ id = 'leftbaraside']/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
        time.sleep(2)
        try:
        # 获取第一张银行卡的金额
            after = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[2]/div/div/div[2]/h4[2]").text
            if flag != 0:
                # 获取第二张银行卡的金额
                time.sleep(1)
                after = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[3]/div/div/div[2]/h4[2]").text
            after = float(after[5:-1])
            before = float(before[5:-1]) - float(money[:-1])
        except:
            after = False
        if before == after:
            result = "T"
        else:
            result = "F"
        if result == expected:
            print("通过")
        else:
            print("不通过！")
            driver.save_screenshot(f"{case}.png")
            time.sleep(1)

        # 退出浏览器
        driver.quit()
        return result

#########################################################################################
###################################        贷款        ###################################

    def Online_loan(self,username, password, payment_password,loan_amount,loan_term,approve,expected,case):
        global result
        driver = webdriver.Chrome()
        #窗口最大化
        driver.maximize_window()
        #跳转网址
        driver.get("http://localhost:90")
        time.sleep(2)
        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        time.sleep(1)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        time.sleep(1)
        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        if approve == "T":
            # 点击银行卡管理
            driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
            time.sleep(1)
            # 获取银行卡账户余额
            num1 = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[2]/div/div/div[2]/h4[2]").text[5:-1]
            num1 = float(num1) + float(loan_amount)
            time.sleep(1)
        else:
            # 点击我的借贷
            driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[4]/ul/li[2]/a").click()
            time.sleep(1)
            try:
                # 获取末页的位置
                ul = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div[3]/nav/ul")
                lis = ul.find_elements(By.XPATH, "li")
                num = len(lis)
                time.sleep(1)
                # 点击末页
                driver.find_element(By.XPATH, f"/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div[3]/nav/ul/li[{num}]/a").click()
                time.sleep(1)
                # 获取当前页面贷款记录数量
                tbody = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div[1]/table/tbody")
                trs = tbody.find_elements(By.XPATH, "tr")
                num1 = len(trs)
            except:
                num1 = "1"
            time.sleep(1)
        # 点击金融工具
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[3]/a").click()
        time.sleep(1)
        # 点击安全网贷
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[3]/ul/li[2]/a").click()
        time.sleep(1)
        # 输入借款金额
        driver.find_element(By.XPATH,
                            "/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[1]/input").send_keys(loan_amount)
        time.sleep(1)
        # 输入借款期限
        driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[3]/input").send_keys(loan_term)
        time.sleep(1)
        # 点击申请
        driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div/form/div[4]/button").click()
        time.sleep(1)
        # 点击确定
        driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").click()
        time.sleep(1)
        try:
            driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button").click()
            time.sleep(1)
        except:
            time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[4]/a").click()
        time.sleep(1)
        if approve == "T":
            self.Loan_review()
            time.sleep(1)
            # 点击银行卡管理
            driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
            time.sleep(1)
            # 获取银行卡账户余额
            num2 = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[2]/div/div/div[2]/h4[2]").text[5:-1]
            num2 = float(num2)
            time.sleep(1)
            if num1 == num2:
                result = "T"
            else:
                result = "F"
        else:
            # 点击我的借贷
            driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[4]/ul/li[2]/a").click()
            time.sleep(1)
            try:
                # 获取末页的位置
                ul = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div[3]/nav/ul")
                lis = ul.find_elements(By.XPATH, "li")
                num = len(lis)
                time.sleep(1)
                # 点击末页
                driver.find_element(By.XPATH, f"/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div[3]/nav/ul/li[{num}]/a").click()
                time.sleep(1)
                # 获取当前页面贷款记录数量
                tbody = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div[1]/table/tbody")
                trs = tbody.find_elements(By.XPATH, "tr")
                num2 = len(trs)
            except:
                num2 = "1"
            time.sleep(1)
            if num1 != num2:
                result = "T"
            else:
                result = "F"
        if result == expected:
            print("通过")
        else:
            print("不通过")
            driver.save_screenshot(f"{case}.png")
            time.sleep(1)

        # 退出浏览器
        driver.quit()
        return result

#########################################################################################
###################################        审核        ###################################

    def Loan_review(self):

        driver = webdriver.Chrome()
        #窗口最大化
        driver.maximize_window()
        time.sleep(2)
        #跳转网址
        driver.get("http://localhost:90")
        time.sleep(1)
        # 输入管理员用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys("admin")
        time.sleep(1)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys("123456")
        time.sleep(1)
        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)
        # 点击网贷银行
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[5]/a").click()
        time.sleep(1)
        # 点击网贷审核
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[5]/ul/li[1]/a").click()
        time.sleep(1)
        # 获取末页的位置
        ul = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[3]/div[2]/nav/ul")
        lis = ul.find_elements(By.XPATH, "li")
        num = len(lis)
        time.sleep(1)
        # 点击末页
        driver.find_element(By.XPATH,
                            f"/html/body/div/div/main/div/div/div/div/div[3]/div[2]/nav/ul//li[{num}]/a").click()
        time.sleep(1)
        # 获取当前页面贷款记录数量
        tbody = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody")
        trs = tbody.find_elements(By.XPATH, "tr")
        num1 = len(trs)
        time.sleep(1)
        # 点击最后一条贷款信息审核通过
        driver.find_element(By.XPATH,
                            f"//html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[{num1}]/td[7]/button[1]").click()
        time.sleep(1)
        # 点击确定
        driver.find_element(By.XPATH,"/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").click()
        time.sleep(1)

        # 退出浏览器
        driver.quit()

